package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.*;
import com.zzyl.nursing.vo.ElderFamilyPageVo;
import com.zzyl.nursing.vo.FimalyVo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FamilyMemberElderServiceImpl  extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService {

    @Autowired
    private IElderService elderService;
    @Autowired
    private IBedService bedService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IRoomService roomService;

    /**
     * 绑定老人
     * @param dto
     */
    @Override
    public void bindingElder(FamilyMemberElderDto dto) {
        //根据老人idCard查询老人信息
        Elder elder = elderService.getOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getIdCardNo, dto.getIdCard()));
        if (ObjectUtil.isEmpty(elder)) {
            throw new RuntimeException("未找到该老人");
        }
        //查询当前登录用户信息
        //获取当前登录用户id
        //判断当前用户id是否已经绑定过该老人
        Long userId = UserThreadLocal.getUserId();
        long count = this.count(Wrappers.<FamilyMemberElder>lambdaQuery()
                .eq(FamilyMemberElder::getElderId, elder.getId()).
                eq(FamilyMemberElder::getFamilyMemberId, userId));
        if (count > 0) {
            throw new RuntimeException("该老人已绑定");
        }
        FamilyMemberElder familyMemberElder = new FamilyMemberElder();
        familyMemberElder.setRemark(dto.getRemark());
        familyMemberElder.setElderId(elder.getId());
        familyMemberElder.setFamilyMemberId(userId);
        this.save(familyMemberElder);

    }

    /**
     * 我的老人
     */
    @Override
    public List<FimalyVo> my() {
        Long userId = UserThreadLocal.getUserId();
        //根据id查询我名下绑定的老人
        List<FamilyMemberElder> list = this.list(Wrappers.<FamilyMemberElder>lambdaQuery().eq(FamilyMemberElder::getFamilyMemberId, userId));
        if (list.isEmpty()){
            return Collections.emptyList();
        }
        List<FimalyVo> collect = list.stream().map(item -> {
            FimalyVo vo = new FimalyVo();
            vo.setId(item.getId());//封装中间表的id
            vo.setElderId(item.getElderId());//封装老人id
            vo.setFamilyMemberId(item.getFamilyMemberId());//封装用户id
            vo.setElderName(elderService.getById(item.getElderId()).getName());//封装老人名称
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 分页查询我的老人
     */
    @Override
    public List<ElderFamilyPageVo> listByPage() {
        Long userId = UserThreadLocal.getUserId();
        List<FamilyMemberElder> list = this.list(Wrappers.<FamilyMemberElder>lambdaQuery().eq(FamilyMemberElder::getFamilyMemberId, userId));
        if (list.isEmpty()){
            return Collections.emptyList();
        }
        List<ElderFamilyPageVo> elderFamilyPageVos = new ArrayList<>();
        for (FamilyMemberElder familyMemberElder : list) {
            ElderFamilyPageVo elderFamilyPageVo = new ElderFamilyPageVo();
            elderFamilyPageVo.setMid(familyMemberElder.getId().toString());
            elderFamilyPageVo.setMremark(familyMemberElder.getRemark());
            Elder elder = elderService.getById(familyMemberElder.getElderId());
            elderFamilyPageVo.setName(elder.getName());
            elderFamilyPageVo.setImage(elder.getImage());
            elderFamilyPageVo.setBedNumber(elder.getBedNumber());
            //根据老人Id查询床位信息
            Bed bed = bedService.getById(elder.getBedId());
            //根据床位信息查询房间信息
            Room room = roomService.getById(bed.getRoomId());
            elderFamilyPageVo.setTypeName(room.getTypeName());
            //根据设备Id查询设备信息
            Device device = deviceService.getOne(Wrappers.<Device>lambdaQuery()
                    .eq(Device::getLocationType, 0)
                    .eq(Device::getBindingLocation, elder.getId())
            );
            elderFamilyPageVo.setIotId(device.getIotId());
            elderFamilyPageVo.setDeviceName(device.getDeviceName());
            elderFamilyPageVo.setProductKey(device.getProductKey());
            elderFamilyPageVos.add(elderFamilyPageVo);
        }
        return elderFamilyPageVos;
    }
}
